/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.br.comps;

import coalescent.br.BRWCompInfo;

import commons.util.FormatUtil;

import java.math.BigInteger;
import java.util.TimerTask;

/**
 * Computes the total number of sample configurations ancestral to a sample configuration by
 * observing its recursion (via the post-order implementation).
 *
 * @author Susanta Tewari
 * @version 1.0 Dec 6, 2010
 */
public class BRWACCounter extends BRWCompAdapter {

    /** Field description */
    private BigInteger count = BigInteger.valueOf(0L);

    /**
     * Total number of sample configurations ancestral (including the sample) to the sample
     * configuration being observed.
     *
     * @return total number of ancestral configurations for the sample configuration being observed
     */
    public BigInteger ancestralConfigsCount() {
        return count;
    }

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onBoundaryCondn(BRWCompInfo event) {
        count = count.add(BigInteger.valueOf(1L));
    }

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onFinishedIteratingEvents(BRWCompInfo event) {
        count = count.add(BigInteger.valueOf(1L));
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public String toString() {

        super.toString();

        return "Total # of ancestral configurations: " + FormatUtil.format(count);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public TimerTask createTimerTask() {

        return new TimerTask() {

            @Override
            public void run() {
                System.out.println("Total # of ancestral configurations processed: " + count);
            }
        };
    }
}
